package com.jiusit.onePurchase.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.jiusit.common.utils.StringUtil;
import com.jiusit.onePurchase.interceptor.UserInterceptor;

@ControllerBind(controllerKey="/share",viewPath="/pages")
/*
 * 分享
 * */
public class ShareController extends ApiController{
	private static final Logger log = Logger
			.getLogger(ShareController.class);

	@Override
	public ApiConfig getApiConfig() {
		ApiConfig ac = new ApiConfig();

		// 配置微信 API 相关常量
		ac.setToken(PropKit.get("token"));
		ac.setAppId(PropKit.get("appId"));
		ac.setAppSecret(PropKit.get("appSecret"));

		/**
		 * 是否对消息进行加密，对应于微信平台的消息加解密方式： 1：true进行加密且必须配置 encodingAesKey
		 * 2：false采用明文模式，同时也支持混合模式
		 */
		ac.setEncryptMessage(PropKit.getBoolean("encryptMessage", false));
		ac.setEncodingAesKey(PropKit.get("encodingAesKey",
				"setting it in config file"));
		return ac;
	}

	public void wxShare(){
		String callback = getPara("jsoncallback"); // 支持Jsonp调用tjr_id
		String reqUrl = getPara("reqUrl");
		String id="";
		String commodity_type="";
		String title="小样乳酸盐欧洲杯大狂欢";
		Map ret = new HashMap();
		/*String imgUrl=PropKit.get("host_uri", "setting it in config file")
				+ "/resource/images/wx/share.jpg";*/
		String imgUrl=PropKit.get("host_uri", "setting it in config file")
				+ "/xyChampion_wx/resource/images/wx/share.jpg";
		try {
			/* 微信JSSDK使用 */
			UUID uuid = UUID.randomUUID();
			String noncestr = uuid.toString().replace("-", "");
			String timestamp = Long.toString(System.currentTimeMillis() / 1000);
			String signStr = new StringBuilder().append(
					"jsapi_ticket=" + TicketUtil.getJssdkTicket().getTicket()
					        +"&noncestr=" + noncestr + "&timestamp="
							+ timestamp + "&url=" + reqUrl).toString();
			log.debug("sign string=" + signStr);

			signStr = HashKit.sha1(signStr);
			log.debug("jssdk sign=" + signStr);
			Map wxConfig = new HashMap();
			wxConfig.put("appId", getApiConfig().getAppId());
			wxConfig.put("nonceStr", noncestr);
			wxConfig.put("timestamp", timestamp);
			wxConfig.put("signature", signStr);

			Map share = new HashMap();
			share.put("title", title);
			share.put("circle_title", "");
			share.put("desc", "看球赛猜胜负赢iPhone6s，小样乳酸盐邀您一起来狂欢，为欧洲杯加油！");
			share.put("circle_desc", "小样乳酸盐欧洲杯大狂欢，小伙伴们快跟我一起看球赛猜胜负赢iPhone6s");
			share.put("link", PropKit.get("host_uri", "setting it in config file")
					+ "/xyChampion_wx/home/homePage");
			share.put("imgUrl",imgUrl);
			ret.put("code", "0");
			ret.put("wxConfig", wxConfig);
			ret.put("share", share);
		} catch (Exception e) {
			ret.put("msg", e.getMessage());
		}

		if (StrKit.isBlank(callback)) {
			renderJson(ret);
		} else {
			renderJson(callback + "(" + JsonKit.toJson(ret) + ")");
		}
	}
	

}
